package cn.tedu.ivos.repair.service.impl;

import cn.tedu.ivos.repair.mapper.RepairMapper;
import cn.tedu.ivos.repair.pojo.dto.RepairParam;
import cn.tedu.ivos.repair.pojo.dto.RepairQuery;
import cn.tedu.ivos.repair.pojo.entity.Repair;
import cn.tedu.ivos.repair.pojo.vo.RepairVO;
import cn.tedu.ivos.repair.service.RepairService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class RepairServiceImpl implements RepairService {
    @Autowired
    RepairMapper repairMapper;
    @Autowired
    VehicleMapper vehicleMapper;
    @Override
    public List<RepairVO> selectAll(RepairQuery repairQuery) {
        log.debug("开始处理【查询报修单】业务，参数：{}",repairQuery);
        List<RepairVO> list = repairMapper.selectAll(repairQuery);
        return list;
    }

    @Override
    public void save(RepairParam repairParam) {
        log.debug("开始处理【保存报修单】业务，参数：{}",repairParam);
        Repair repair = new Repair();
        BeanUtils.copyProperties(repairParam,repair);
        if(repair.getId()==null){
            Vehicle vehicle = new Vehicle();
            vehicle.setId(repair.getVehicleId());
            vehicle.setStatus("3");
            vehicle.setUpdateTime(new Date());
            vehicleMapper.updateVehicle(vehicle);
            repair.setCreateTime(new Date());
            repairMapper.insert(repair);
        }else {
            repair.setUpdateTime(new Date());
            repairMapper.update(repair);
        }
    }
}
